package com.ebt.m.wiki.util;

import com.ebt.m.utils.android.DateUtils;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;


public class AgeObj implements Comparable<AgeObj> {

    /**
     * 年龄
     */
    private int age;
    /**
     * 单位
     */
    private AgeUnitEnum unit;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public AgeUnitEnum getUnit() {
        return unit;
    }

    public void setUnit(AgeUnitEnum unit) {
        this.unit = unit;
    }

    /**
     * @param ageStr 计算年龄（12：12岁； 0.08：8个月。0.00030：30天）
     * @return
     * @deprecated 把年龄格式转化为年龄对象<br/>
     * 如果返回的想的年龄==-1，则说明转化失败。
     */
    public static AgeObj parseAgeObj2(String ageStr) {
        AgeObj ageObj = new AgeObj();
        int obj = -1;
        if (ageStr.contains(".")) {
            String[] ss = ageStr.split("\\.");
            if (ss[1].length() == 2) {
                obj = Integer.parseInt(ss[1].substring(0, 2));
                ageObj.setUnit(AgeUnitEnum.month);
            } else if (ss[1].length() == 5) {
                obj = Integer.parseInt(ss[1].substring(2, 5));
                ageObj.setUnit(AgeUnitEnum.day);
            }
        } else {
            obj = Integer.parseInt(ageStr);
            ageObj.setUnit(AgeUnitEnum.year);
        }
        ageObj.setAge(obj);
        return ageObj;
    }

    /**
     * 把年龄格式转化为年龄对象<br/>
     * 如果返回null，则说明转化失败。
     *
     * @param ageStr 计算年龄（0.00030）小数点后固定为5位
     * @return
     */
    public static AgeObj parseAgeObj(String ageStr) {
        AgeObj ageObj = new AgeObj();
        int obj = -1;
        String[] ss = ageStr.split("\\.");
        if (ss.length <= 1) {
            ageObj.setAge(Integer.parseInt(ss[0]));
            ageObj.setUnit(AgeUnitEnum.year);
            return ageObj;
        } else {
            if (ss[1].length() < 5) {
                ageObj.setAge(Integer.parseInt(ss[0]));
                ageObj.setUnit(AgeUnitEnum.year);
                return ageObj;
            }
            obj = Integer.parseInt(ss[0]);
            boolean t = true;
            if (obj == 0) {
                obj = Integer.parseInt(ss[1].substring(0, 2));
                if (obj == 0) {
                    obj = Integer.parseInt(ss[1].substring(2, 5));
                    if (obj == 0) {
                        t = false;
                    } else {
                        ageObj.setUnit(AgeUnitEnum.day);
                    }
                } else {
                    ageObj.setUnit(AgeUnitEnum.month);
                }
            } else {
                ageObj.setUnit(AgeUnitEnum.year);
            }
            if (t == false) {
                //特殊处理：0.00000
                ageObj.setAge(0);
                ageObj.setUnit(AgeUnitEnum.year);
            } else {
                ageObj.setAge(obj);
            }
        }

        return ageObj;
    }

    /**
     * 小于、等于或大于指定对象，则分别返回负整数、零或正整数
     */
    public static int compareTo(Date birthday, AgeObj obj) {
        if (obj.getUnit() == AgeUnitEnum.year) {
            int year = DateUtils.getAge(birthday);
            if (year > obj.getAge()) {
                return 1;
            } else if (year == obj.getAge()) {
                return 0;
            } else if (year < obj.getAge()) {
                return -1;
            }
        } else if (obj.getUnit() == AgeUnitEnum.month) {
            GregorianCalendar calendar = new GregorianCalendar();
            calendar.setTime(new Date());
            calendar.add(Calendar.MONTH, -obj.getAge());
            Date tempDate = calendar.getTime();
            return DateUtils.compareDate(birthday, tempDate);
        } else if (obj.getUnit() == AgeUnitEnum.day) {
            GregorianCalendar calendar = new GregorianCalendar();
            calendar.setTime(new Date());
            calendar.add(Calendar.DAY_OF_MONTH, -obj.getAge());
            Date tempDate = calendar.getTime();
            return DateUtils.compareDate(birthday, tempDate);
        }
        return 1;
    }

//    public static Date getDate(AgeObj obj) {
//        int unit = -100;
//        if (obj.getUnit() == AgeUnitEnum.year) {
//            unit = Calendar.YEAR;
//        } else if (obj.getUnit() == AgeUnitEnum.month) {
//            unit = Calendar.MONTH;
//        } else if (obj.getUnit() == AgeUnitEnum.day) {
//            unit = Calendar.DAY_OF_MONTH;
//        }
//        GregorianCalendar calendar = new GregorianCalendar();
//        calendar.setTime(new Date());
//        calendar.add(unit, -obj.getAge());
//        return calendar.getTime();
//    }

    /**
     * 判断comparedDate是否在startDate和endDate之间
     *
     * @param startDate
     * @param endDate
     * @param comparedDate
     * @return
     */
    public static boolean betweenDate(Date startDate, Date endDate, Date comparedDate) {
        int a = DateUtils.compareDate(comparedDate, startDate);
        int b = DateUtils.compareDate(endDate, comparedDate);
        return (a >= 0) && (b >= 0);
    }

    /**
     * 小于、等于或大于指定对象，则分别返回负整数、零或正整数
     */
    @Override
    public int compareTo(AgeObj o) {
        // TODO Auto-generated method stub
        int result = -1;
        switch (this.unit) {
            case year:
                if (o.unit == AgeUnitEnum.year) {
                    if (this.age > o.age) {
                        result = 1;
                    } else if (this.age < o.age) {
                        result = -1;
                    } else {
                        result = 0;
                    }
                } else if (o.unit == AgeUnitEnum.month) {
                    int temp = this.age * 12;
                    if (temp > o.age) {
                        result = 1;
                    } else if (temp < o.age) {
                        result = -1;
                    } else {
                        result = 0;
                    }
                } else if (o.unit == AgeUnitEnum.day) {
                    int temp = this.age * 12 * 365;
                    if (temp > o.age) {
                        result = 1;
                    } else if (temp < o.age) {
                        result = -1;
                    } else {
                        result = 0;
                    }
                }
                break;
            case month:
                if (o.unit == AgeUnitEnum.year) {
                    int temp = o.age * 12;
                    if (this.age > temp) {
                        result = 1;
                    } else if (this.age < temp) {
                        result = -1;
                    } else {
                        result = 0;
                    }
                } else if (o.unit == AgeUnitEnum.month) {
                    if (this.age > o.age) {
                        result = 1;
                    } else if (this.age < o.age) {
                        result = -1;
                    } else {
                        result = 0;
                    }
                } else if (o.unit == AgeUnitEnum.day) {
                    int temp = this.age * 30;
                    if (temp > o.age) {
                        result = 1;
                    } else if (temp < o.age) {
                        result = -1;
                    } else {
                        result = 0;
                    }
                }
                break;
            case day:
                if (o.unit == AgeUnitEnum.year) {
                    int temp = o.age * 12 * 365;
                    if (this.age > temp) {
                        result = 1;
                    } else if (this.age < temp) {
                        result = -1;
                    } else {
                        result = 0;
                    }
                } else if (o.unit == AgeUnitEnum.month) {
                    int temp = o.age * 30;
                    if (this.age > temp) {
                        result = 1;
                    } else if (this.age < temp) {
                        result = -1;
                    } else {
                        result = 0;
                    }
                } else if (o.unit == AgeUnitEnum.day) {
                    if (this.age > o.age) {
                        result = 1;
                    } else if (this.age < o.age) {
                        result = -1;
                    } else {
                        result = 0;
                    }
                }
                break;
            default:
                break;
        }
        return result;
    }
}
